package net.fiodor.geom {
	import flash.display.Stage;	
	import flash.geom.Point;	
	import flash.display.MovieClip;	
	
	import net.fiodor.utils.MathUtils;	
	
	/**
	 * @author Tomasz Swierszcz, 
	 * email: tomasz.swierszcz@dreamlab.pl
	 */
	public class Vector {
		
		public var x:Number;
		public var y:Number;
		
	
	public function Vector( $x:Number, $y:Number ) {
		
		if( arguments.length == 0 ) {
			
			x = y = 0;	
			
		}
		else if( arguments.length == 1 ) {
			
			x= y = $x;
			
		}
		else {
			
			x = $x;
			y = $y;	
			
		}
		
	}
	
	public static function convert( v:Vector, from:MovieClip, to:MovieClip ):Vector {
		
			var point : Point = new Point(v.x, v.y);
		
			from.localToGlobal( point );
			if( to ) to.globalToLocal( point );
			return v;
				
	}
	
	public static function getScreenCenter():Vector {
		
		return new Vector( Stage.width, Stage.height ).scaleNew( .5 );	
		
	}
		
	public function set length( l:Number ):void {
		
		var len:Number = length;
		if( len ) scale( l/len );
		else x = l;
		
	}
	
	public function get length():Number {
		
		
		return Math.sqrt( x*x + y*y );
		
	}
	
	public function get radians():Number {
		
		return Math.atan2( y, x );	
		
	}
	
	public function get angle():Number {
	
		return MathUtils.rad2deg( radians );	
		
	}
	
	public function getClone():Vector {
		
		
		return new Vector( x, y );	
		
	}
	
	public function reset( $x:Number, $y:Number ):void {
		
		x = $x; y = $y;	
		
	}
	
	
	public function toString():String {
		
		
		return "[ "+ x + ", " + y + " ]";
		
		
	}
	
	public function plus( v:Vector ):void {
		
		x += v.x;
		y += v.y;	
		
	}
	
	public function plusNew( v:Vector ):Vector {
		
	
		return new Vector( x+v.x, y+v.y );	
		
	}
	
	public function minus( v:Vector ):void {
		
		x -= v.x;
		y -= v.y;	
		
	}
	
	public function minusNew( v:Vector ):Vector {
		
	
		return new Vector( x-v.x, y-v.y );	
		
	}
	
	public function negate():void {
	
		x = -x;
		y = -y;	
		
	}
	
	
	public function negateNew():Vector {
		
	
		return new Vector( -x, -y );	
		
	}
	
	public function scale( s:Number ):void {
		
		x *= s;
		y *= s;	
		
	}
	
	public function scaleNew( s:Number ):Vector {
		
		
		return new Vector( s*x, s*y );	
		
	}
	
	public function round():Vector {
		
		return new Vector( Math.round( x ), Math.round( y ) );	
		
	}
	
	public function floor():Vector {
		
		return new Vector( Math.floor( x ), Math.floor( y ) );	
		
	}
	
	public function ceil():Vector {
		
		return new Vector( Math.ceil( x ), Math.ceil( y ) );	
		
	}
}
	
}